#[test]
fn basic_control() {
    let x = 5;

    let y = if x == 5 { 10 } else { 15 };

    println!("x = {:?}, y = {:?}", x, y);

    println!("for loops");
    for i in 0..5 {
        println!("i = {:?}", i);
    }
    println!("iter loop");
    for i in [0, 1, 2].iter() {
        println!("i = {:?}", i);
    }
}

#[test]
fn loop_demo() {
    'outer: loop {
        println!("Entered the outer loop");

        loop {
            println!("Entered the inner loop");
            break 'outer;
        }

        // println!("This point will never be reached");
    }

    println!("Exited the outer loop");
}

#[test]
fn match_demo() {
    let day = 5;

    match day {
        0 | 6 => println!("weekend"),
        e @ 1..=5 => println!("{} is weekday", e),
        _ => println!("invalid"),
    };

    let x = 5;
    let mut y = 5;

    match x {
        // the `r` inside the match has the type `&i32`
        ref r => println!("Got a reference to {}", r),
    }

    match y {
        // the `mr` inside the match has the type `&i32` and is mutable
        ref mut mr => {
            println!("Got a mutable reference to {}", mr);
            *mr += 6;
            println!("update mutable reference to {}", mr);
        }
    }
}

#[test]
fn advanced_match() {
    struct Point {
        x: i32,
        y: i32,
    }

    let origin = Point { x: 0, y: 0 };

    match origin {
        Point { x, y: 1 } => println!("y=1, x is {}", x),
        Point { x, .. } => println!("y!=1, x is {}", x),
    }

    enum OptionalInt {
        Value(i32),
        Missing,
    }

    let x = OptionalInt::Value(5);
    let _y = OptionalInt::Missing;
    match x {
        // 这里是 match 的 if guard 表达式，我们将在以后的章节进行详细介绍
        OptionalInt::Value(i) if i > 5 => println!("Got an int bigger than five!"),
        OptionalInt::Value(..) => println!("Got an int!"),
        OptionalInt::Missing => println!("No such luck."),
    }
}

#[test]
fn if_let_demo() {
    let number = Some(7);
    let mut optional = Some(0);

    // If `let` destructures `number` into `Some(i)`, evaluate the block.
    if let Some(i) = number {
        println!("Matched {:?}!", i);
    } else {
        println!("Didn't match a number!");
    }

    // While `let` destructures `optional` into `Some(i)`, evaluate the block.
    while let Some(i) = optional {
        if i > 9 {
            println!("Greater than 9, quit!");
            optional = None;
        } else {
            println!("`i` is `{:?}`. Try again.", i);
            optional = Some(i + 1);
        }
    }
}
